/**
 * 
 */
package top.lcpsky.flashcards.common.factory;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.StringUtils;

import top.lcpsky.flashcards.common.page.Page;
import top.lcpsky.flashcards.common.state.Order;


/**   
 * @ClassName:  PageFactory   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: lcp
 * @date:   2020年7月8日 下午3:33:28   
 *     
 * @Copyright: 2020 www.ankept.com Inc. All rights reserved. 
 */
public class PageFactory<T> {

    public Page<T> defaultPage() {
        HttpServletRequest request = HttpContext.getRequest();
        int limit = Integer.valueOf(request.getParameter("limit"));     //每页多少条数据
        int offset = Integer.valueOf(request.getParameter("offset"));   //每页的偏移量(本页当前有多少条)
        String sort = request.getParameter("sort");         //排序字段名称
        String order = request.getParameter("order");       //asc或desc(升序或降序)
        if (StringUtils.isEmpty(sort)) {
            Page<T> page = new Page<>((offset / limit + 1), limit);
            page.setOpenSort(false);
            return page;
        } else {
            Page<T> page = new Page<>((offset / limit + 1), limit, sort);
            if (Order.ASC.getDes().equals(order)) {
                page.setAsc(true);
            } else {
                page.setAsc(false);
            }
            return page;
        }
    }
}
